home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-11-03 | 9.6 KB | 436 lines |
- package com.symantec.itools.lang;
-
-
- import java.io.IOException;
- import java.io.File;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
- import com.symantec.itools.io.FileSystem;
- import com.symantec.itools.util.StringTokenizer;
-
-
- /**
- * @author Symantec Internet Tools Division
- * @version 1.0
- * @since VCafe 3.0
- */
-
- public class Classpath
- {
- /**
- * @since VCafe 3.0
- */
- protected Hashtable filesTables;
-
- /**
- * @since VCafe 3.0
- */
- protected String[] actualClasspath;
-
- /**
- * @since VCafe 3.0
- */
- protected String[] validClasspath;
-
- /**
- * @since VCafe 3.0
- */
- protected ClasspathEntry[] validEntries;
-
- /**
- * @since VCafe 3.0
- */
- protected Hashtable actualEntriesTable;
-
- {
- filesTables = new Hashtable();
- }
-
- public Classpath()
- {
- this(System.getProperty("java.class.path"));
- }
-
- public Classpath(String cp)
- {
- setClasspath(cp);
- }
-
- /**
- * @param cp TODO
- * @since VCafe 3.0
- */
- public void setClasspath(String cp)
- {
- Vector list;
- String[] array;
-
- array = new StringTokenizer(cp, File.pathSeparator).getTokens();
- actualClasspath = new String[array.length];
-
- for(int i = 0; i < array.length; i++)
- {
- actualClasspath[i] = FileSystem.getCanonicalPath(array[i], true);
- }
-
- array = FileSystem.getValidPath(cp, true);
- list = new Vector();
-
- for(int i = 0; i < array.length; i++)
- {
- if(!(list.contains(array[i])))
- {
- list.addElement(array[i]);
- }
- }
-
- validClasspath = new String[list.size()];
- list.copyInto(validClasspath);
-
- validEntries = new ClasspathEntry[validClasspath.length];
-
- for(int i = 0; i < validEntries.length; i++)
- {
- validEntries[i] = new ClasspathEntry(validClasspath[i]);
- }
-
- actualEntriesTable = new Hashtable();
-
- for(int i = 0; i < actualClasspath.length; i++)
- {
- String type;
-
- if(new File(actualClasspath[i]).isFile())
- {
- type = "ZIP";
- }
- else
- {
- type = "FILE";
- }
-
- actualEntriesTable.put(type + i, new ClasspathEntry(actualClasspath[i]));
- }
- }
-
- /**
- * @since VCafe 3.0
- */
- public String[] getActualClasspath()
- {
- return (actualClasspath);
- }
-
- /**
- * @since VCafe 3.0
- */
- public String[] getValidClasspath()
- {
- return (validClasspath);
- }
-
- /**
- * @param url TODO
- * @since VCafe 3.0
- */
- public ClasspathEntry getEntry(URL url)
- {
- String key;
-
- if(url == null)
- {
- return (null);
- }
-
- key = url.getFile();
-
- if(!key.startsWith("/ZIP") && !key.startsWith("/FILE"))
- {
- return (null);
- }
-
- return (getEntry(key.substring(1, key.indexOf('/', 1))));
- }
-
- /**
- * @param entry TODO
- * @since VCafe 3.0
- */
- public boolean contains(String entry)
- {
- entry = FileSystem.getCanonicalPath(entry, true);
-
- for(int i = 0; i < actualClasspath.length; i++)
- {
- if(FileSystem.compareFilenames(actualClasspath[i], entry))
- {
- return (true);
- }
- }
-
- return (false);
- }
-
- /**
- * @param entry TODO
- * @since VCafe 3.0
- */
- public boolean isValid(String entry)
- {
- entry = FileSystem.getCanonicalPath(entry, true);
-
- for(int i = 0; i < validClasspath.length; i++)
- {
- if(FileSystem.compareFilenames(validClasspath[i], entry))
- {
- return (true);
- }
- }
-
- return (false);
- }
-
- /**
- * @param key TODO
- * @since VCafe 3.0
- */
- public ClasspathEntry getEntry(String key)
- {
- return ((ClasspathEntry)actualEntriesTable.get(key));
- }
-
- /**
- * @param file TODO
- * @exception com.symantec.itools.lang.NotJavaNameException
- * @since VCafe 3.0
- */
- public JavaName convertFileNameToJavaName(File file)
- throws NotJavaNameException
- {
- return (convertFileNameToJavaName(FileSystem.getCanonicalPath(file, true)));
- }
-
- /**
- * @param fileName TODO
- * @exception com.symantec.itools.lang.NotJavaNameException
- * @since VCafe 3.0
- */
- public JavaName convertFileNameToJavaName(String fileName)
- throws NotJavaNameException
- {
- for(int i = 0; i < validEntries.length; i++)
- {
- if(validEntries[i].isDirectory())
- {
- String dir;
-
- dir = validEntries[i].getName();
-
- if(FileSystem.isInPath(fileName, dir))
- {
- return (new JavaName(fileName.substring(dir.length()).replace(File.separatorChar, '/')));
- }
- }
- }
-
- return (new JavaName(fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1)));
- }
-
- /**
- * @since VCafe 3.0
- */
- public void load()
- {
- for(int i = 0; i < validEntries.length; i++)
- {
- if(filesTables.get(validEntries[i].getName()) == null)
- {
- filesTables.put(validEntries[i].getName(), validEntries[i].load(this));
- }
- }
- }
-
- /**
- * @param entryName TODO
- * @since VCafe 3.0
- */
- public ClasspathEntry find(String entryName)
- {
- for(int i = 0; i < validEntries.length; i++)
- {
- if(validEntries[i].find(entryName))
- {
- return (validEntries[i]);
- }
- }
-
- return (null);
- }
-
- /**
- * @since VCafe 3.0
- */
- public String getActualClasspathAsString()
- {
- StringBuffer cp;
- int i;
-
- cp = new StringBuffer();
-
- for(i = 0; i < actualClasspath.length - 1; i++)
- {
- cp.append(actualClasspath[i]).append(File.pathSeparatorChar);
- }
-
- cp.append(actualClasspath[i]);
-
- return (cp.toString());
- }
-
- /**
- * @since VCafe 3.0
- */
- public String getValidClasspathAsString()
- {
- StringBuffer cp;
- int i;
-
- cp = new StringBuffer();
-
- for(i = 0; i < validClasspath.length - 1; i++)
- {
- cp.append(validClasspath[i]).append(File.pathSeparatorChar);
- }
-
- cp.append(validClasspath[i]);
-
- return (cp.toString());
- }
-
- /**
- * @param entry TODO
- * @since VCafe 3.0
- */
- public Classpath removeEntry(String entry)
- {
- int index;
-
- entry = FileSystem.getCanonicalPath(entry, true);
-
- for(index = 0; index < validClasspath.length; index++)
- {
- if(FileSystem.compareFilenames(entry, validClasspath[index]))
- {
- break;
- }
- }
-
- if(index <= validClasspath.length)
- {
- StringBuffer tempClasspath;
-
- tempClasspath = new StringBuffer();
-
- for(int i = 0; i < validClasspath.length; i++)
- {
- if(i != index)
- {
- tempClasspath.append(validClasspath[i]).append(File.pathSeparatorChar);
- }
- }
-
- return (new Classpath(tempClasspath.toString()));
- }
-
- return (this);
- }
-
- /**
- * @param entry TODO
- * @since VCafe 3.0
- */
- public Classpath prependEntry(String entry)
- {
- return (new Classpath(entry + File.pathSeparatorChar + getValidClasspathAsString()));
- }
-
- /**
- * @param entry TODO
- * @since VCafe 3.0
- */
- public Classpath appendEntry(String entry)
- {
- return (new Classpath(getValidClasspathAsString() + File.pathSeparatorChar + entry));
- }
-
- /**
- * @since VCafe 3.0
- */
- public String[] getValidEntries()
- {
- String[] list;
-
- list = new String[validClasspath.length];
-
- for(int i = 0; i < list.length; i++)
- {
- list[i] = validClasspath[i];
- }
-
- return (list);
- }
-
- /**
- * @since VCafe 3.0
- */
- public String[] getInvalidEntries()
- {
- Vector tempList;
- String[] list;
-
- tempList = new Vector();
-
- for(int i = 0; i < actualClasspath.length; i++)
- {
- boolean found;
-
- found = false;
-
- for(int j = 0; j < validClasspath.length; j++)
- {
- if(validClasspath[j].equals(actualClasspath[i]))
- {
- found = true;
- break;
- }
- }
-
- if(!(found))
- {
- tempList.addElement(actualClasspath[i]);
- }
- }
-
- list = new String[tempList.size()];
- tempList.copyInto(list);
-
- return (list);
- }
-
- public void cleanup()
- {
- for(int i = 0; i < validEntries.length; i++)
- {
- validEntries[i].cleanup();
- }
- }
-
- protected void finalize()
- throws Throwable
- {
- super.finalize();
- cleanup();
- }
- }